﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class KyXBDAO
    {
        #region 1. Them
        public static bool ThemKyXB(KyXBDTO kyxb){
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaKyXB", kyxb.MaKyXB));
                sqlParams.Add(new SqlParameter("@MaBaoTC", kyxb.MaBaoTC));
                sqlParams.Add(new SqlParameter("@NgayPhatHanh", kyxb.NgayPhatHanh));
                sqlParams.Add(new SqlParameter("@Gia", kyxb.Gia));
                sqlParams.Add(new SqlParameter("@SoLuongNhap", kyxb.SoLuongNhap));
                sqlParams.Add(new SqlParameter("@SoLuongTon", kyxb.SoLuongTon));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_InsertKYXB", sqlParams);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        #endregion
        #region 2. Sua
        public static bool SuaKyXB(KyXBDTO kyxb)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaKyXB", kyxb.MaKyXB));
                sqlParams.Add(new SqlParameter("@MaBaoTC", kyxb.MaBaoTC));
                sqlParams.Add(new SqlParameter("@NgayPhatHanh", kyxb.NgayPhatHanh));
                sqlParams.Add(new SqlParameter("@Gia", kyxb.Gia));
                sqlParams.Add(new SqlParameter("@SoLuongNhap", kyxb.SoLuongNhap));
                sqlParams.Add(new SqlParameter("@SoLuongTon", kyxb.SoLuongTon));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_UpdateKYXB", sqlParams);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
        #region 3. Xoa
        public static bool XoaKyXB(String makyxb){
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaKyXB", makyxb));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_DeleteKYXB", sqlParams);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        #endregion
        #region 4. Tim kiem
        public static KyXBDTO TimKyXB(String makyxb)
        {
            KyXBDTO result = new KyXBDTO();
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaKyXB", makyxb));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectKYXB", sqlParams);
                DataRow dr = dt.Rows[0];

                result.MaKyXB = dr["MaKyXB"].ToString();
                result.MaBaoTC = dr["MaBaoTC"].ToString();
                result.NgayPhatHanh = DateTime.Parse(dr["NgayPhatHanh"].ToString());
                result.Gia = decimal.Parse(dr["Gia"].ToString());
                result.SoLuongNhap = int.Parse(dr["SoLuongNhap"].ToString());
                result.SoLuongTon = int.Parse(dr["SoLuongTon"].ToString());

            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        public static DataTable TimTatCaKyXB(){
            DataTable dt= new DataTable();
            try
            {
                 dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectKYXBsAll");              
                
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return dt;
        }
        public static List<KyXBDTO> TimKyXBCuaBaoTC(String mabaotc){
            List<KyXBDTO> result = new List<KyXBDTO>();
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaBaoTC", mabaotc));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectKYXBsByAndMaBaoTC", sqlParams);
                foreach (DataRow dr in dt.Rows)
                {
                    KyXBDTO tmp = new KyXBDTO();

                    tmp.MaKyXB = dr["MaKyXB"].ToString();
                    tmp.MaBaoTC = dr["MaBaoTC"].ToString();
                    tmp.NgayPhatHanh = DateTime.Parse(dr["NgayPhatHanh"].ToString());
                    tmp.Gia = decimal.Parse(dr["Gia"].ToString());
                    tmp.SoLuongNhap = int.Parse(dr["SoLuongNhap"].ToString());
                    tmp.SoLuongTon = int.Parse(dr["SoLuongTon"].ToString());
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        public static bool CheckKyXBByID(String makyxb){
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaKyXB", makyxb));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectKYXB", sqlParams);
                if (dt.Rows.Count > 0)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return result;
        }
        #endregion
    }
}
